package com.building.hcrm.wcf;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;

import android.content.Context;

import com.building.hcrm.base.BaseParam;
import com.building.hcrm.base.BaseServiceProxy;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.DateDeserializer;
import com.building.hcrm.common.HttpHelper;
import com.building.hcrm.common.User;
import com.building.hcrm.model.PlanDetail;
import com.building.hcrm.model.SignLog;
import com.building.hcrm.model.SysMenu;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.model.SysUserParam;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class UserServiceProxy extends BaseServiceProxy
{
	private static UserServiceProxy _instance;

	public static UserServiceProxy getInstance(Context context)
	{
		if (_instance == null)
		{
			_instance = new UserServiceProxy();
		}
		_instance._context = context;
		return _instance;
	}

	public User UserValidInfo(SysUserParam param) throws Exception
	{
		User re = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "UserValidInfo", strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "UserValidInfoResult");
			for (String str : lstRe)
			{
				re = gson.fromJson(str, User.getInstance(_context).getClass());
			}
		}
		return re;
	}

	public String UserReset(String userNum, String userPwd, String newPwd) throws Exception
	{
		String re = null;

		String strPost = String.format("{\"userNum\":\"%1$s\",\"userPwd\":\"%2$s\",\"newPwd\":\"%3$s\"}", userNum, userPwd, newPwd);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "UserReset", strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "UserResetResult");
			for (String str : lstRe)
			{
				re = str;
			}
		}
		return re;
	}

	public List<SysUser> UserOfBranchAndSelf(BaseParam param) throws Exception
	{
		List<SysUser> re = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "UserOfBranchAndSelf",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "UserOfBranchAndSelfResult");
			for (String str : lstRe)
			{
				re = new ArrayList<SysUser>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++)
				{
					SysUser su = new SysUser();
					su = gson.fromJson(ja.getString(i), su.getClass());
					re.add(su);
				}
			}
		}

		return re;
	}
	
	public List<SysUser> UserOfBranch(BaseParam param) throws Exception
	{
		List<SysUser> re = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "UserOfBranch",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "UserOfBranchResult");
			for (String str : lstRe)
			{
				re = new ArrayList<SysUser>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++)
				{
					SysUser su = new SysUser();
					su = gson.fromJson(ja.getString(i), su.getClass());
					re.add(su);
				}
			}
		}

		return re;
	}

	public List<SysMenu> MenuLoad(BaseParam param) throws Exception
	{
		List<SysMenu> re = null;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "MenuLoad", strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "MenuLoadResult");
			for (String str : lstRe)
			{
				re = new ArrayList<SysMenu>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++)
				{
					SysMenu sm = new SysMenu();
					sm = gson.fromJson(ja.getString(i), sm.getClass());
					re.add(sm);
				}
			}
		}

		return re;
	}
}
